import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router, ActivatedRoute } from '@angular/router';

import { CommonService } from '../../../services-common/common.service';
import { LoveRecycManageService } from '../love-recyc-manage.service';

@Component({
  selector: 'app-love-model-select',
  templateUrl: './love-model-select.component.html',
  styleUrls: ['./love-model-select.component.scss']
})
export class LoveModelSelectComponent implements OnInit {

  routeParams: any = {};

  seachParams: any = {
    name: '',
    typeId: '',
    status: ''
  };
  // 分类下拉
  classOptions: any = [];

  tableParams: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0
  };
  tableData: any = [];

  constructor(
    private message: NzMessageService,
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private commonService: CommonService,
    private services: LoveRecycManageService
  ) {
    activatedRoute.queryParams.subscribe((res: any) => {
      this.routeParams = res || {};
    })
  }

  async ngOnInit() {
    // 分类下拉
    await this.getClassOptions();

    if (this.routeParams.id || this.routeParams.id == 0) {
      this.seachParams.typeId = Number(this.routeParams.id);
    }
    
    this.loadTable();
  }

  // 获取分类下拉
  async getClassOptions() {
    try {
      const res: any = await this.services.getClassSelectList({});
      if (res.code == 0) {
        this.classOptions = res.data || [];
      } else {
        this.message.error(res.message);
      }
    } catch (error: any) {
      this.message.error(error);
    }
  }

  // Table Data
  async loadTable() {
    try {
      let params: any = {
        ...this.seachParams,
        pageNum: this.tableParams.page,
        pageSize: this.tableParams.pageSize
      }
      for (let key in params) {
        params[key] = this.commonService.isEmpty(params[key]) ? '' : params[key];
      }

      this.tableParams.tableLoading = true;
      const res: any = await this.services.getModelSelectList(params);
      this.tableParams.tableLoading = false;

      if (res.code == 0) {
        const { records, total } = res.data || {};
        this.tableData = records || [];
        this.tableParams.total = total || 0;
      } else {
        this.message.error(res.message);
      }
    } catch (error: any) {
      this.message.error(error);
    }
  }

  seachData() {
    this.tableParams.page = 1;
    this.loadTable();
  }
  resetData() {
    Object.keys(this.seachParams).forEach(key => {
      this.seachParams[key] = '';
    })
    this.tableParams.page = 1;
    this.loadTable();
  }

  async deleteItem(item: any) {
    try {
      const res: any = await this.services.modelSelectDelete({ id: item.id });
      if (res.code == 0) {
        this.message.success('删除成功');
        this.loadTable();
      } else {
        this.message.error(res.message || '删除失败');
      }
    } catch (error: any) {
      this.message.error(error);
    }
  }

  /**
   * 新增 | 编辑
   * @param item 
   */
  addUpdate(item?: any) {
    let params: any = {
      type: item ? 1 : 0, // 0：新增，1：编辑
    }
    if (item) {
      params.typeId = item.typeId
    }
    
    this.router.navigate(['/aisi-recyc/loveModselectAdd'], {
      queryParams: params
    })
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // 页数改变时的回调函数
  onPageSizeChange(sizeNum: number) {
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }
}
